<template>
  <div class="tab-title-page withdrawal">
    <div class="common_form_search">
      <el-form
        ref="searchForm"
        :inline="true"
        :model="searchForm"
      >
        <el-form-item label="订单号">
          <el-input
            style="width:220px"
            v-model="searchForm.orderNo"
            placeholder="请输入订单号"
            clearable
          ></el-input>
        </el-form-item>
        <el-form-item label="商品名称">
          <el-input
            v-model="searchForm.goodsName"
            placeholder="请输入商品名称"
            clearable
          ></el-input>
        </el-form-item>
        <el-form-item label="下单姓名">
          <el-input
            v-model="searchForm.receivingName"
            placeholder="请输入下单姓名"
            clearable
          ></el-input>
        </el-form-item>
        <el-form-item label="下单电话">
          <el-input
            v-model="searchForm.receivingMobile"
            placeholder="请输入下单电话"
            clearable
          ></el-input>
        </el-form-item>
        <el-form-item label="订单状态">
          <el-select
            v-model="searchForm.orderStatus"
            placeholder="全部"
            clearable
          >
            <el-option
              v-for="item in selectList"
              :key="item.id"
              :label="item.name"
              :value="item.id"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="第几期">
          <el-select
            v-model="searchForm.leaseTerm"
            placeholder="全部"
            clearable
          >
            <el-option
              v-for="item in 12"
              :key="item"
              :label="`第${item}期`"
              :value="item+''"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="下单日">
          <my-date-picker ref="makeOrderPicker"></my-date-picker>
        </el-form-item>
        <el-form-item label="预计扣款日">
          <my-date-picker ref="withholdPicker"></my-date-picker>
        </el-form-item>
        <el-form-item label="实际支付日">
          <my-date-picker ref="payPicker"></my-date-picker>
        </el-form-item>
        <el-form-item label="退款时间">
          <my-date-picker ref="refundPicker"></my-date-picker>
        </el-form-item>
        <el-form-item label="供应商名称">
          <el-input
            v-model="searchForm.shopName"
            placeholder="请输入供应商名称"
            clearable
          ></el-input>
        </el-form-item>
        <el-form-item>
          <el-button
            size="small"
            type="primary"
            @click="search()"
          >查询</el-button>
          <el-button
            size="small"
            @click="reset()"
          >重置</el-button>
        </el-form-item>
      </el-form>
    </div>
    <el-divider class="middle_divider"></el-divider>
    <div class="child_page">
      <div class="flex align-center flex-between table-top-btns">
        <div class="flex align-center">
          <h5 class="flex align-center">
            <svg-icon
              style="margin-right:15px"
              icon-class="list"
              :size="15"
            />数据列表
          </h5>
          <el-button
            v-if="$hasMethod('#export')"
            size="small"
            icon="el-icon-upload2"
            class="export-btn"
            @click="exportFile"
          >导出</el-button>
        </div>
        <el-popover
          placement="top"
          width="570"
          offset="50"
          trigger="click"
        >
          <el-table :data="stateData">
            <el-table-column
              width="150"
              prop="title"
              label="表头标题"
            ></el-table-column>
            <el-table-column
              prop="content"
              label="对应内容"
            ></el-table-column>
          </el-table>
          <p
            class="color-666 state_btn pointer"
            slot="reference"
          ><i class="el-icon-warning-outline"></i>注释说明</p>
        </el-popover>
      </div>
      <el-table
        border
        :data="tableData"
      >
        <el-table-column
          fixed
          width="60"
          align="center"
          prop="order_id"
          label="编号"
        />
        <el-table-column
          fixed
          width="190"
          align="center"
          label="订单号"
        >
          <template slot-scope="scope">
            <span
              class="blue-font pointer"
              @click="goToOrder(scope.row.trade_no)"
            >{{scope.row.trade_no}}</span>
          </template>
        </el-table-column>
        <el-table-column
          align="center"
          width="200"
          prop="goods_title"
          label="商品名称"
        />
        <el-table-column
          align="center"
          prop="realname"
          label="姓名"
        />
        <el-table-column
          align="center"
          width="120"
          prop="mobile"
          label="电话"
        />
        <el-table-column
          align="center"
          width="160"
          prop="create_time"
          label="下单日"
        />
        <el-table-column
          align="center"
          prop="month_total"
          label="总租期"
        />
        <el-table-column
          align="center"
          label="月租金"
        >
          <template slot-scope="scope">
            <span>{{scope.row.month_rent&&'￥'+scope.row.month_rent}}</span>
          </template>
        </el-table-column>
        <template v-for="i in 12">
          <el-table-column
            :key="`t${i}`"
            align="center"
            width="100"
            :label="`回款日(${i})`"
          >
            <template slot-scope="scope">
              <span>{{scope.row.lease[i].begin_time}}</span>
            </template>
          </el-table-column>
          <el-table-column
            :key="`m${i}`"
            align="center"
            :label="`回款金额(${i})`"
          >
            <template slot-scope="scope">
              <span>{{scope.row.lease[i].rent&&'￥'+scope.row.lease[i].rent}}</span>
            </template>
          </el-table-column>
          <el-table-column
            :key="`d${i}`"
            align="center"
            width="100"
            :label="`结算日(${i})`"
          >
            <template slot-scope="scope">
              <span>{{scope.row.lease[i].settle_time}}</span>
            </template>
          </el-table-column>
          <el-table-column
            :key="`p${i}`"
            align="center"
            :label="`结算金额(${i})`"
          >
            <template slot-scope="scope">
              <span>{{scope.row.lease[i].settle_amount&&'￥'+scope.row.lease[i].settle_amount}}</span>
            </template>
          </el-table-column>
        </template>
        <el-table-column
          align="center"
          label="累计应收"
        >
          <template slot-scope="scope">
            <span>{{scope.row.rent_total&&'￥'+scope.row.rent_total}}</span>
          </template>
        </el-table-column>
        <el-table-column
          align="center"
          label="累计应付"
        >
          <template slot-scope="scope">
            <span>{{scope.row.total_pay&&'￥'+scope.row.total_pay}}</span>
          </template>
        </el-table-column>
        <el-table-column
          align="center"
          label="欠收金额"
        >
          <template slot-scope="scope">
            <span>{{scope.row.owe_pay&&'￥'+scope.row.owe_pay}}</span>
          </template>
        </el-table-column>
        <el-table-column
          fixed="right"
          align="center"
          prop="shopname"
          label="供应商名称"
        />
      </el-table>
      <custom-page
        ref="customPage"
        :total="total"
        @getList="getList"
      ></custom-page>
    </div>
  </div>
</template>

<script>
import customPage from "@/components/customPage";
import myDatePicker from "@/components/myDatePicker";
import { getOrderStatus, financeCollectionList, collectionListExport } from "@/api/finance";
export default {
  components: { customPage, myDatePicker },
  data() {
    return {
      searchForm: {
        orderNo: '',
        goodsName: '',
        receivingName: '',
        receivingMobile: '',
        orderStatus: '',
        leaseTerm: '',
        orderCreateTime: '',
        leaseTime: '',
        orderPayTime: '',
        refundTime: '',
        shopName: ''
      },
      tableData: [],
      total: 0,
      selectList: [],
      stateData: [
        { title: '回款日2', content: '订单第二期回款日期，以此类推' },
        { title: '回款金额2', content: '订单第二期回款金额，以此类推' },
        { title: '累计应收', content: '累计应收(总租金)=商品数量*总租期*月租金' },
        { title: '累计应付', content: '截止到当日客户应缴的租金=商品数量*月租金*当月应付期数' },
        { title: '欠收金额', content: '截止到当日客户应付未付的租金=累计应付-已支付的租金' }
      ]
    }
  },
  methods: {
    goToOrder(orderId) {
      if (orderId) {
        this.$router.push({ name: 'orderList', params: { orderId } });
      }
    },
    // 获取数据
    getList() {
      // flag为true, 则不刷新表格
      const { page, pageSize } = this.$refs.customPage;
      financeCollectionList({
        page,
        pageSize,
        ...this.searchForm
      }).then(res => {
        this.tableData = res.data || [];
        this.total = res.total || 0;
      })
    },
    // 查询
    search() {
      const pickerVal1 = this.$refs.makeOrderPicker.getVal();
      const pickerVal2 = this.$refs.withholdPicker.getVal();
      const pickerVal3 = this.$refs.payPicker.getVal();
      const pickerVal4 = this.$refs.refundPicker.getVal();
      if (pickerVal1 == 'error' || pickerVal2 == 'error' || pickerVal3 == 'error' || pickerVal4 == 'error') {
        return;
      }
      this.searchForm.orderCreateTime = pickerVal1;
      this.searchForm.leaseTime = pickerVal2;
      this.searchForm.orderPayTime = pickerVal3;
      this.searchForm.refundTime = pickerVal4;
      this.$refs.customPage.page = 1;
      this.getList();
    },
    // 重置
    reset() {
      if (this.$refs.makeOrderPicker) {
        this.$refs.makeOrderPicker.reset();
      }
      if (this.$refs.withholdPicker) {
        this.$refs.withholdPicker.reset();
      }
      if (this.$refs.payPicker) {
        this.$refs.payPicker.reset();
      }
      if (this.$refs.refundPicker) {
        this.$refs.refundPicker.reset();
      }
      this.searchForm = {
        orderNo: '',
        goodsName: '',
        receivingName: '',
        receivingMobile: '',
        orderStatus: '',
        leaseTerm: '',
        orderCreateTime: '',
        leaseTime: '',
        orderPayTime: '',
        refundTime: '',
        shopName: ''
      };
      this.getList();
    },
    // 导出
    exportFile() {
      collectionListExport(this.searchForm);
    },
    getSelect() {
      getOrderStatus().then(res => {
        const list = [];
        for (let key in res) {
          list.push({
            id: key,
            name: res[key]
          })
        }
        this.selectList = list || [];
      })
    },
  },
  mounted() {
    this.getSelect();
    this.getList();
  }
}
</script>

<style lang="scss" scoped>
.common_form_search {
  padding: 15px 15px 0 15px;
  background-color: #ffffff;
  border-radius: 0 0 10px 10px;
}
.child_page {
  padding: 15px;
}
.over_address {
  text-overflow: -o-ellipsis-lastline;
  overflow: hidden;
  text-overflow: ellipsis;
  display: -webkit-box;
  -webkit-line-clamp: 4;
  line-clamp: 4;
  -webkit-box-orient: vertical;
}
.state_btn {
  position: relative;
  padding: 5px;
  border: 1px solid #e6e6e6;
  i {
    font-size: 14px;
    margin-right: 5px;
  }
}
</style>